<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.num" placeholder="产品编号" style="width: 150px;" class="filter-item" />
      <el-input v-model="listQuery.name" placeholder="产品名称" style="width: 150px;" class="filter-item" />
      <el-select v-model="listQuery.supplyId" placeholder="供应商" clearable style="width: 150px" class="filter-item">
        <el-option v-for="item in supplies" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-select v-model="listQuery.warehouseId" placeholder="仓库名称" clearable style="width: 150px" class="filter-item">
        <el-option v-for="item in warehouses" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-refresh-left" @click="handleReset">
        重置
      </el-button>
      <el-button v-if="1 === 2" class="filter-item fr" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
      <!-- 图片上传-->
      <el-popover
        placement="right"
        width="400"
        trigger="click"
      >
        <el-upload
          ref="upload"
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          :auto-upload="false"
        >
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        <el-button slot="reference" class="filter-item fr" icon="el-icon-edit" style="margin-right: 30px">上传图片</el-button>
      </el-popover>
    </div>

    <el-table :data="tableData" stripe max-height="600">
      <el-table-column prop="num" label="产品编号" />
      <el-table-column prop="name" label="产品名称" />
      <el-table-column prop="proType" label="产品类别" />
      <el-table-column prop="proNum" label="入库数量" />
      <el-table-column prop="proSupName" label="产品供应商" />
      <el-table-column prop="wareId" label="仓库号" />
      <el-table-column prop="wareAddr" label="仓库地址" />
      <el-table-column prop="wareTel" label="仓库联系人" />
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click.native.prevent="deleteRow(scope.$index, tableData[scope.$index])"
          >
            入库
          </el-button>
          <el-button
            v-if="1 === 2"
            size="mini"
            type="text"
            @click.native.prevent="handleUpdate(scope.$index, tableData)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 新增/编辑 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <!--        <el-form-item label="Type" prop="type">-->
        <!--          <el-select v-model="temp.type" class="filter-item" placeholder="Please select">-->
        <!--            <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="Date" prop="timestamp">-->
        <!--          <el-date-picker v-model="temp.timestamp" type="datetime" placeholder="Please pick a date" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="Title" prop="title">-->
        <!--          <el-input v-model="temp.title" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="Status">-->
        <!--          <el-select v-model="temp.status" class="filter-item" placeholder="Please select">-->
        <!--            <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item" />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="Imp">-->
        <!--          <el-rate v-model="temp.importance" :colors="['#99A9BF', '#F7BA2A', '#FF9900']" :max="3" style="margin-top:8px;" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="Remark">-->
        <!--          <el-input v-model="temp.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { wareAll, inwarePro, upImg } from '@/api'
import Pagination from '@/components/Pagination'

export default {
  name: 'ImWarehouse',
  components: {
    Pagination
  },
  data() {
    return {
      fileList: [{ name: '',
        url: '' }],
      listQuery: {
        num: '', // 产品编号
        name: '', // 产品名称
        supplyId: '', // 供应商id
        warehouseId: '', // 仓库id
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined

      },
      supplies: [
        {
          id: '111',
          name: '阿里'
        }
      ],
      warehouses: [
        {
          id: '111',
          name: '1号仓库'
        }
      ],
      tableData: [],
      total: 100,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑入库',
        create: '新增入库'
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.inWare()
  },
  methods: {
    // 筛选
    handleFilter() {
      console.log('筛选')
    },
    // 重置
    handleReset() {
      console.log('重置')
    },
    // 入库
    deleteRow(index, row) {
      console.log('入库' + row.id)
      inwarePro({ id: row.id }).then(res => {
        this.$message(res.message)
        let idx
        for (let i = 0; i < this.tableData.length; i++) {
          if (this.tableData[i].id === row.id) {
            idx = i
            break
          }
        }
        this.tableData.splice(idx, 1)
      }).catch(err => {
        this.$message(err.message)
      })
    },
    // 入库列表
    inWare() {
      wareAll().then(res => {
        this.tableData = res.data
        if (res.data == null) {
          this.$message(res.message)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 重置表单
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    // 点击新增
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    // 新增入库
    createData() {
      console.log('新增入库')
      this.dialogFormVisible = false
      // this.$refs['dataForm'].validate((valid) => {
      //   if (valid) {
      //     this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
      //     this.temp.author = 'vue-element-admin'
      //     createArticle(this.temp).then(() => {
      //       this.list.unshift(this.temp)
      //       this.dialogFormVisible = false
      //       this.$notify({
      //         title: 'Success',
      //         message: 'Created Successfully',
      //         type: 'success',
      //         duration: 2000
      //       })
      //     })
      //   }
      // })
    },
    // 点击编辑
    handleUpdate(row) {
      // this.temp = Object.assign({}, row) // copy obj
      // this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    // 编辑入库
    updateData() {
      console.log('编辑入库')
      this.dialogFormVisible = false
      // this.$refs['dataForm'].validate((valid) => {
      //   if (valid) {
      //     const tempData = Object.assign({}, this.temp)
      //     tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
      //     updateArticle(tempData).then(() => {
      //       const index = this.list.findIndex(v => v.id === this.temp.id)
      //       this.list.splice(index, 1, this.temp)
      //       this.dialogFormVisible = false
      //       this.$notify({
      //         title: 'Success',
      //         message: 'Update Successfully',
      //         type: 'success',
      //         duration: 2000
      //       })
      //     })
      //   }
      // })
    },
    // 分页事件
    getList() {
      console.log('分页事件')
      // fetchList(this.listQuery).then(response => {
      //   this.list = response.data.items
      //   this.total = response.data.total
      //
      //   // Just to simulate the time of the request
      //   setTimeout(() => {
      //     this.listLoading = false
      //   }, 1.5 * 1000)
      // })
    },
    // 图片上传
    submitUpload() {
      this.$refs.upload.submit()
      console.log(this.$refs.upload.submit())
      // eslint-disable-next-line no-undef
      upImg(this.fileList).then(res => {
        this.$message(res.message)
      }).catch(err => {
        this.$message(err.message)
      })
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    }
  }
}
</script>

